package tp.pr3;

import tp.pr3.items.CodeCard;


public class Street {
	private Place source;
	private Place target;
	private Direction direction;
	private java.lang.String code;
	private boolean isOpen;


	public Street(Place source,
		      Direction direction,
		      Place target)
	{
	 this.source=source;
	 this.target=target;
	 this.direction=direction;
	 code=null;
	 isOpen=true;
	}
	
	public Street(Place source,
		      Direction direction,
		      Place target,
		      boolean isOpen,
		      java.lang.String code)
		{
			this.source=source;
			this.target=target;
			this.direction=direction;
			this.code=code;
			this.isOpen=isOpen;
		}
	
	public boolean comeOutFrom(Place place,
            Direction where)
		{
			if(source==place && direction==where)return true;
			else if(target==place && where==Direction.reverseDirection(direction))return true;
			else return false;
		}	
	
	public Place nextPlace(Place whereAmI)
	{
	 if (whereAmI!=source && whereAmI!=target ) return null;
	 else
	 	{
		 if(whereAmI==source)return target;
		 else return source;
	 	}
	}
	
	public boolean isOpen()
		{
			return this.isOpen;
		}
	
	public boolean open(CodeCard card)
		{
			if(card.getCode().equals(this.code))
			{
				this.isOpen=true;
			}
			return isOpen==true;
		}
	
	public boolean close(CodeCard card)
	{
		if(card.getCode().equals(this.code))
		{
			this.isOpen=false;
		}
		return isOpen==false;
	}	

	public boolean compareCode(CodeCard card)
	{
		return this.code.equals(card.getCode());
	}
}
